home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter25 / isohex25_6 / isohex25_6.cpp < prev    next >
C/C++ Source or Header  |  2000-12-17  |  10KB  |  412 lines

  1. /*****************************************************************************
  2. IsoHex25_6.cpp
  3. Ernest S. Pazera
  4. 08DEC2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib, d3d8.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. Needs D3DFuncs.h/cpp
  10. *****************************************************************************/
  11.  
  12. //////////////////////////////////////////////////////////////////////////////
  13. //INCLUDES
  14. //////////////////////////////////////////////////////////////////////////////
  15. #define WIN32_LEAN_AND_MEAN  
  16.  
  17. #include <windows.h>   
  18. #include <math.h>//sin and cos
  19. #include "GDICanvas.h"
  20. #include "ddraw.h"
  21. #include "DDFuncs.h"
  22. #include "d3d.h"
  23. #include "d3dfuncs.h"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //DEFINES
  27. //////////////////////////////////////////////////////////////////////////////
  28. //name for our window class
  29. #define WINDOWCLASS "ISOHEX25"
  30. //title of the application
  31. #define WINDOWTITLE "IsoHex 25-6"
  32.  
  33. //screen attributes
  34. const DWORD SCREENWIDTH=640;
  35. const DWORD SCREENHEIGHT=480;
  36. const DWORD SCREENBPP=16;
  37.  
  38. //tile dimensions
  39. const DWORD TILEWIDTH=64;
  40. const DWORD TILEHEIGHT=32;
  41.  
  42. //map dimensions
  43. const DWORD MAPWIDTH=20;
  44. const DWORD MAPHEIGHT=40;
  45.  
  46.  
  47.  
  48. //////////////////////////////////////////////////////////////////////////////
  49. //PROTOTYPES
  50. //////////////////////////////////////////////////////////////////////////////
  51. bool Prog_Init();//game data initalizer
  52. void Prog_Loop();//main game loop
  53. void Prog_Done();//game clean up
  54.  
  55. //////////////////////////////////////////////////////////////////////////////
  56. //GLOBALS
  57. //////////////////////////////////////////////////////////////////////////////
  58. HINSTANCE hInstMain=NULL;//main application handle
  59. HWND hWndMain=NULL;//handle to our main window
  60.  
  61. //IDirectDraw7 Pointer
  62. LPDIRECTDRAW7 lpdd=NULL;
  63.  
  64. //surfaces
  65. LPDIRECTDRAWSURFACE7 lpddsPrime=NULL;
  66. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  67. LPDIRECTDRAWSURFACE7 lpddsTexture=NULL;
  68. LPDIRECTDRAWSURFACE7 lpddsMouseMap=NULL;
  69.  
  70. //IDirect3D7
  71. LPDIRECT3D7 lpd3d=NULL;
  72.  
  73. //IDirect3DDevice
  74. LPDIRECT3DDEVICE7 lpd3ddev=NULL;
  75.  
  76. //vertices
  77. D3DTLVERTEX vert[4];//three vertices
  78.  
  79. //////////////////////////////////////////////////////////////////////////////
  80. //WINDOWPROC
  81. //////////////////////////////////////////////////////////////////////////////
  82. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  83. {
  84.     //which message did we get?
  85.     switch(uMsg)
  86.     {
  87.     case WM_KEYDOWN:
  88.         {
  89.             //check for escape key
  90.             if(wParam==VK_ESCAPE)
  91.             {
  92.                 DestroyWindow(hWndMain);
  93.             }
  94.  
  95.             return(0);//handled message
  96.         }break;
  97.     case WM_DESTROY://the window is being destroyed
  98.         {
  99.  
  100.             //tell the application we are quitting
  101.             PostQuitMessage(0);
  102.  
  103.             //handled message, so return 0
  104.             return(0);
  105.  
  106.         }break;
  107.     case WM_PAINT://the window needs repainting
  108.         {
  109.             //a variable needed for painting information
  110.             PAINTSTRUCT ps;
  111.             
  112.             //start painting
  113.             HDC hdc=BeginPaint(hwnd,&ps);
  114.  
  115.             /////////////////////////////
  116.             //painting code would go here
  117.             /////////////////////////////
  118.  
  119.             //end painting
  120.             EndPaint(hwnd,&ps);
  121.                         
  122.             //handled message, so return 0
  123.             return(0);
  124.         }break;
  125.     }
  126.  
  127.     //pass along any other message to default message handler
  128.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  129. }
  130.  
  131.  
  132. //////////////////////////////////////////////////////////////////////////////
  133. //WINMAIN
  134. //////////////////////////////////////////////////////////////////////////////
  135. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  136. {
  137.     //assign instance to global variable
  138.     hInstMain=hInstance;
  139.  
  140.     //create window class
  141.     WNDCLASSEX wcx;
  142.  
  143.     //set the size of the structure
  144.     wcx.cbSize=sizeof(WNDCLASSEX);
  145.  
  146.     //class style
  147.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  148.  
  149.     //window procedure
  150.     wcx.lpfnWndProc=TheWindowProc;
  151.  
  152.     //class extra
  153.     wcx.cbClsExtra=0;
  154.  
  155.     //window extra
  156.     wcx.cbWndExtra=0;
  157.  
  158.     //application handle
  159.     wcx.hInstance=hInstMain;
  160.  
  161.     //icon
  162.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  163.  
  164.     //cursor
  165.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  166.  
  167.     //background color
  168.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  169.  
  170.     //menu
  171.     wcx.lpszMenuName=NULL;
  172.  
  173.     //class name
  174.     wcx.lpszClassName=WINDOWCLASS;
  175.  
  176.     //small icon
  177.     wcx.hIconSm=NULL;
  178.  
  179.     //register the window class, return 0 if not successful
  180.     if(!RegisterClassEx(&wcx)) return(0);
  181.  
  182.     //create main window
  183.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  184.  
  185.     //error check
  186.     if(!hWndMain) return(0);
  187.  
  188.     //if program initialization failed, then return with 0
  189.     if(!Prog_Init()) return(0);
  190.  
  191.     //message structure
  192.     MSG msg;
  193.  
  194.     //message pump
  195.     for(;;)    
  196.     {
  197.         //look for a message
  198.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  199.         {
  200.             //there is a message
  201.  
  202.             //check that we arent quitting
  203.             if(msg.message==WM_QUIT) break;
  204.             
  205.             //translate message
  206.             TranslateMessage(&msg);
  207.  
  208.             //dispatch message
  209.             DispatchMessage(&msg);
  210.         }
  211.  
  212.         //run main game loop
  213.         Prog_Loop();
  214.     }
  215.     
  216.     //clean up program data
  217.     Prog_Done();
  218.  
  219.     //return the wparam from the WM_QUIT message
  220.     return(msg.wParam);
  221. }
  222.  
  223. //////////////////////////////////////////////////////////////////////////////
  224. //INITIALIZATION
  225. //////////////////////////////////////////////////////////////////////////////
  226. bool Prog_Init()
  227. {
  228.     lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
  229.  
  230.     //set the display mode
  231.     lpdd->SetDisplayMode(SCREENWIDTH,SCREENHEIGHT,SCREENBPP,0,0);
  232.  
  233.     //create primary surface
  234.     lpddsPrime=LPDDS_CreatePrimary3D(lpdd,1);
  235.  
  236.     //create back buffer
  237.     lpddsBack=LPDDS_GetSecondary3D(lpddsPrime);
  238.     
  239.     //get the idirect3d pointer
  240.     lpd3d=LPD3D_Create(lpdd);
  241.  
  242.     //create the idirect3ddevice(hack method)
  243.     lpd3ddev=LPD3DDEV_Create(lpd3d,lpddsBack);
  244.  
  245.     //set up viewport
  246.     LPD3DDEV_SetViewport(lpd3ddev,0,0,SCREENWIDTH,SCREENHEIGHT);
  247.  
  248.     //load in texture image
  249.     CGDICanvas gdic;
  250.     gdic.Load(NULL,"texture.bmp");
  251.  
  252.     //load texture
  253.     lpddsTexture=LPDDS_CreateTexture(lpdd,gdic.GetWidth(),gdic.GetHeight());
  254.  
  255.     //grab texture's DC
  256.     HDC hdc;
  257.     lpddsTexture->GetDC(&hdc);
  258.  
  259.     //blit image
  260.     BitBlt(hdc,0,0,gdic.GetWidth(),gdic.GetHeight(),gdic,0,0,SRCCOPY);
  261.  
  262.     //release texture's dc
  263.     lpddsTexture->ReleaseDC(hdc);
  264.  
  265.     //set this texture
  266.     lpd3ddev->SetTexture(0,lpddsTexture);
  267.  
  268.     //set up extra rendering target
  269.     DDSURFACEDESC2 ddsd;
  270.     DDSD_Clear(&ddsd);
  271.     ddsd.dwFlags=DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
  272.     ddsd.dwWidth=SCREENWIDTH;
  273.     ddsd.dwHeight=SCREENHEIGHT;
  274.     ddsd.ddsCaps.dwCaps=DDSCAPS_3DDEVICE;
  275.     lpdd->CreateSurface(&ddsd,&lpddsMouseMap,NULL);
  276.  
  277.  
  278.     return(true);//return success
  279. }
  280.  
  281. //////////////////////////////////////////////////////////////////////////////
  282. //CLEANUP
  283. //////////////////////////////////////////////////////////////////////////////
  284. void Prog_Done()
  285. {    
  286.     //release mousemap
  287.     LPDDS_Release(&lpddsMouseMap);
  288.  
  289.     //release texture
  290.     LPDDS_Release(&lpddsTexture);
  291.  
  292.     //release IDirect3DDevice
  293.     LPD3DDEV_Release(&lpd3ddev);
  294.  
  295.     //release IDirect3D 
  296.     LPD3D_Release(&lpd3d);
  297.  
  298.     //clean up primary surface(this will clean up the back buffer, also)
  299.     LPDDS_Release(&lpddsPrime);
  300.  
  301.     //clean up the dd pointer
  302.     LPDD_Release(&lpdd);
  303. }
  304.  
  305. //////////////////////////////////////////////////////////////////////////////
  306. //MAIN GAME LOOP
  307. //////////////////////////////////////////////////////////////////////////////
  308. void Prog_Loop()
  309. {
  310.     //set render target to mousemap
  311.     lpd3ddev->SetRenderTarget(lpddsMouseMap,0);
  312.  
  313.     //set texture
  314.     lpd3ddev->SetTexture(0,NULL);
  315.  
  316.     //clear the viewport to black
  317.     lpd3ddev->Clear(0,NULL,D3DCLEAR_TARGET,0,0,0);
  318.  
  319.     //start the scene
  320.     lpd3ddev->BeginScene();
  321.  
  322.     //center positions
  323.     D3DVALUE CenterX,CenterY;
  324.     int x,y;
  325.     DWORD dwColor;
  326.     DWORD dwRow;
  327.     DWORD red,green,blue;
  328.  
  329.     //loop through map
  330.     for(y=0;y<MAPHEIGHT;y++)
  331.     {
  332.         for(x=0;x<MAPWIDTH;x++)
  333.         {
  334.             //calculate world coordinates for center of tile
  335.             CenterX=(float)(x*TILEWIDTH+(y&1)*(TILEWIDTH/2));
  336.             CenterY=(float)(y*(TILEHEIGHT/2));
  337.  
  338.             //row calculation
  339.             dwRow=y+1;
  340.  
  341.             //convert to color(only works for 565 RGB)
  342.             blue=(dwRow & 0x1F)<<3;
  343.             green=((dwRow>>5) & 0x3F)<<2;
  344.             red=((dwRow>>11)&0x1F)<<3;
  345.             dwColor=(0xFF<<24) + (red<<16) + (green<<8) + blue;
  346.  
  347.             //set up the vertex
  348.             //v1
  349.             VERTEX_Set(&vert[0],CenterX-TILEWIDTH/2,CenterY,dwColor,0.0,0.0);
  350.             //v2
  351.             VERTEX_Set(&vert[1],CenterX,CenterY-TILEHEIGHT/2,dwColor,0.0,0.0);
  352.             //v3
  353.             VERTEX_Set(&vert[2],CenterX,CenterY+TILEHEIGHT/2,dwColor,0.0,0.0);
  354.             //v4
  355.             VERTEX_Set(&vert[3],CenterX+TILEWIDTH/2,CenterY,dwColor,0.0,0.0);
  356.  
  357.             //render the triangle strip
  358.             LPD3DDEV_DrawTriangleStrip(lpd3ddev,vert,4);
  359.         }
  360.     }
  361.  
  362.     //end the scene
  363.     lpd3ddev->EndScene();
  364.  
  365.     //set render target to back buffer
  366.     lpd3ddev->SetRenderTarget(lpddsBack,0);
  367.  
  368.     //set texture
  369.     lpd3ddev->SetTexture(0,lpddsTexture);
  370.  
  371.     //clear the viewport to black
  372.     lpd3ddev->Clear(0,NULL,D3DCLEAR_TARGET,0,0,0);
  373.  
  374.     //start the scene
  375.     lpd3ddev->BeginScene();
  376.  
  377.     //loop through map
  378.     for(y=0;y<MAPHEIGHT;y++)
  379.     {
  380.         for(x=0;x<MAPWIDTH;x++)
  381.         {
  382.             //calculate world coordinates for center of tile
  383.             CenterX=(float)(x*TILEWIDTH+(y&1)*(TILEWIDTH/2));
  384.             CenterY=(float)(y*(TILEHEIGHT/2));
  385.  
  386.             //set up the vertex
  387.             //v1
  388.             VERTEX_Set(&vert[0],CenterX-TILEWIDTH/2,CenterY,D3DRGB(1.0,1.0,1.0),0.0,0.0);
  389.             //v2
  390.             VERTEX_Set(&vert[1],CenterX,CenterY-TILEHEIGHT/2,D3DRGB(1.0,1.0,1.0),1.0,0.0);
  391.             //v3
  392.             VERTEX_Set(&vert[2],CenterX,CenterY+TILEHEIGHT/2,D3DRGB(1.0,1.0,1.0),0.0,1.0);
  393.             //v4
  394.             VERTEX_Set(&vert[3],CenterX+TILEWIDTH/2,CenterY,D3DRGB(1.0,1.0,1.0),1.0,1.0);
  395.  
  396.             //render the triangle strip
  397.             LPD3DDEV_DrawTriangleStrip(lpd3ddev,vert,4);
  398.         }
  399.     }
  400.  
  401.     //end the scene
  402.     lpd3ddev->EndScene();
  403.  
  404.     //show the mousemap
  405.     //comment out this line to show regular map
  406.     lpddsBack->Blt(NULL,lpddsMouseMap,NULL,DDBLT_WAIT,NULL);
  407.  
  408.     //flip 
  409.     lpddsPrime->Flip(NULL,DDFLIP_WAIT);
  410. }
  411.  
  412.